<?php
namespace Home\Controller;

class BossController extends HomeController
{
    //主页列表
    public function index()
    {
        //查部门,在搜索里显示
        $arr = M('group')->select();
        $group = [];
        foreach ($arr as $k => $v) {
            $group[$v['id']] = $v['groupname'];
        }
        //搜索分页显示一起
        $m = M('user');
        //搜索
        $keywordN = '%' . $_GET['n'] . '%';
        //获取搜索关键字
        $where['name'] = array('like', $keywordN);
        if ($_GET['g']) {
            $keywordG = $_GET['g'];
            //获取搜索关键字
            $where['groupid'] = array('eq', $keywordG);
        }
        //分页 这个函数在app下的com下com里
        $p = getpage($m, $where, 7);
        $lista = $m->field(true)->where($where)->select();
        foreach ($lista as $k => $v) {
            //把部门添加到数组中
            $gid = $v['groupid'];
            $infog = M('group')->field('groupname')->where(array('id' => array('eq', $gid)))->select();
            $lista[$k]['gid'] = $infog[0];
            //把职位添加到数组
            $pid = $v['uposition'];
            $infop = M('roleposition')->field('remark')->where(array('position' => array('eq', $pid)))->select();
            $lista[$k]['pid'] = $infop[0];
        }
        // dump($lista);exit;
        $this->assign('name1', $name1['name']);
        $this->assign('group', $group);
        $this->assign('lista', $lista);
        $this->page = $p->show();
        $this->display();
    }
    //详情页面
    public function zwtiaozheng()
    {
        //接收传来的ID
        $id = I('get.id');
        $list = M('user')->where(array('id' => array('eq', $id)))->select();
        $arr = M('roleposition')->select();
        $zhiwei = [];
        foreach ($arr as $k => $v) {
            $zhiwei[$v['position']] = $v['remark'];
        }
        $this->assign('list', $list);
        $this->assign('zhiwei', $zhiwei);
        $this->display();
    }
    public function edit()
    {
        $pass = M('user');
        $where = 'id=' . $_POST['id'];
        $data = $_POST['uposition'];
        // dump($data);exit;
        $quanxian = $_POST['username'];
        // dump($quanxian);exit;
        if ($quanxian == 'admin1' || $data == 1) {
            $this->error("你无权修改!");
        } else {
            if ($pass->where($where)->setField('uposition', $data) >= 0) {
                $this->success("修改成功！", U('Boss/index'));
            } else {
                $this->error("修改失败");
            }
        }
    }
    public function bmtiaozheng()
    {
        $id = I('get.id');
        $list = M('user')->where(array('id' => array('eq', $id)))->select();
        $arr = M('group')->select();
        $bumen = [];
        foreach ($arr as $k => $v) {
            $bumen[$v['id']] = $v['groupname'];
        }
        $this->assign('list', $list);
        $this->assign('bumen', $bumen);
        $this->display();
    }
    public function bumen()
    {
        $pass = M('user');
        $data = $_POST['groupid'];
        $quanxian = $_POST['username'];
        $where = 'id=' . $_POST['id'];
        // dump($quanxian);exit;
        if ($quanxian == 'admin1') {
            $this->error("你无权修改!");
        } else {
            if ($pass->where($where)->setField('groupid', $data) >= 0) {
                $this->success("修改成功！", U('Boss/index'));
            } else {
                $this->error("修改失败");
            }
        }
    }
    public function fire()
    {
        $id = $_GET['id'];
        $where = 'uid = ' . $id;
        // dump($id);
        //    dump($where);exit;
        if ($id == 1) {
            $this->error("无法删除ROOT用户!");
        } else {
            if (M('user')->delete($id) > 0 && M('userdetail')->where($where)->delete() > 0) {
                $this->success("删除成功！", U('Boss/lzgl'));
            } else {
                $this->error("删除失败");
            }
        }
    }
    public function doAjax()
    {
        if (!IS_AJAX) {
            $this->error('你是不是迷路了.....');
            exit;
        }
        //搜索分页显示一起
        $m = M('user');
        if ($_POST['username']) {
            $keywordN = '%' . I('post.username') . '%';
            //获取搜索关键字
            $where['name'] = array('like', $keywordN);
            if ($_POST['g']) {
                $keywordG = I('post.g');
                //获取搜索关键字
                $where['groupid'] = array('eq', $keywordG);
            }
        }
        //搜索
        if ($_POST['g']) {
            $keywordG = I('post.g');
            //获取搜索关键字
            $where['groupid'] = array('eq', $keywordG);
            if ($_POST['username']) {
                $keywordN = '%' . I('post.username') . '%';
                //获取搜索关键字
                $where['name'] = array('like', $keywordN);
            }
        }
        //分页 这个函数在app下的com下com里
        $p = getpage($m, $where, 7);
        $lista = $m->field(true)->where($where)->select();
        foreach ($lista as $k => $v) {
            $url1 = "bmtiaozheng/groupid/" . $v['groupid'] . "/id/" . $v['id'];
            $url3 = "zwtiaozheng/id/" . $v['id'];
            $url2[0] = $url1;
            $url4[0] = $url3;
            //把部门添加到数组中
            $gid = $v['groupid'];
            $lista[$k]['url'] = $url2[0];
            $lista[$k]['zwtz'] = $url4[0];
            $infog = M('group')->field('groupname')->where(array('id' => array('eq', $gid)))->select();
            $lista[$k]['gid'] = $infog[0];
            //把职位添加到数组
            $pid = $v['uposition'];
            $infop = M('roleposition')->field('remark')->where(array('position' => array('eq', $pid)))->select();
            $lista[$k]['pid'] = $infop[0];
        }
        $data = $lista;
        $this->ajaxReturn($data);
    }
    public function lzgl()
    {
        $m = M('user');
        $lista = $m->select();
        $this->assign('lista', $lista);
        $this->display();
    }
}